/*
 * Alloy Analyzer
 * Copyright (c) 2004 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.gui;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import alloy.ast.Command;
import alloy.ast.RunCommand;

/** This is the parent class of all objects which represent a line in the analysis table.
 */

abstract class Analysis {
    private String _name;
    private final ModelPanel _textWindow;
    private final String _modelString;
    public final Command.Info _commandInfo;
    private final Date _startTime;
    private final long _durationMillis;

    private AlloyWindow modelFrame;

    private List listenerList;

    Analysis(
	     String name_,
	     ModelPanel textWindow_,
	     String modelString_,
	     Command.Info commandInfo_,
	     Date startTime_,
	     long durationMillis_) {
	_name = name_;
	_textWindow = textWindow_;
	_modelString = modelString_;
	_commandInfo = commandInfo_;
	_startTime = startTime_;
	_durationMillis = durationMillis_;

	listenerList = new ArrayList();
    }

    String getName() {
	return _name;
    }

    ModelPanel getFilePanel() {
	return _textWindow;
    }

    Date getStartTime() {
	return _startTime;
    }

    long getDurationMillis() {
	return _durationMillis;
    }

    void setName(String name_) {
	_name = name_;
	if (modelFrame != null)
	    modelFrame.setTitle(name_ + " - Alloy Source");
	fireNameChanged(name_);
    }

    void openModelFrame() {
	if (modelFrame == null) {
	    modelFrame = new TextWindow(getName() + " - Alloy Source", _modelString);
	    modelFrame.setBounds(0, 0, 500, 300);
	    modelFrame.positionNicely();
	    modelFrame.setDefaultCloseOperation(TextWindow.HIDE_ON_CLOSE);
	    modelFrame
		.addWindowListener(
				   (WindowListener)AlloySwingUtilities
				   .listener(new WindowAdapter() {
					   public void windowClosing(WindowEvent e) {
					       closeModelFrame();
					   }
				       }));
	}

	modelFrame.show();
    }

    void closeModelFrame() {
	if (modelFrame != null)
	    modelFrame.setVisible(false);
    }

    void releaseResources() {
	closeModelFrame();
	if (modelFrame != null) {
	    modelFrame.dispose();
	    modelFrame = null;
	}
	listenerList.clear();
    }

    void addAnalysisListener(AnalysisListener l) {
	listenerList.add(l);
    }

    void removeAnalysisListener(AnalysisListener l) {
	listenerList.remove(l);
    }

    private void fireNameChanged(String name) {
	AnalysisListener[] listeners =
	    (AnalysisListener[])listenerList.toArray(new AnalysisListener[0]);
	AnalysisEvent event = new AnalysisEvent(this, name);

	for (int i = listeners.length - 1; i>=0; i--) {
	    listeners[i].nameChanged(event);
	}
    }
}

